package org.dromara.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.SysReportConfigBo;
import org.dromara.system.domain.vo.SysReportConfigVo;
import org.dromara.system.domain.SysReportConfig;
import org.dromara.system.mapper.SysReportConfigMapper;
import org.dromara.system.service.ISysReportConfigService;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.util.function.Function;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 报配置Service业务层处理
 *
 * @author yilong.fu
 * @date 2024-02-27
 */
@RequiredArgsConstructor
@Service
public class SysReportConfigServiceImpl implements ISysReportConfigService {

    private final SysReportConfigMapper baseMapper;

    /**
   * 查询报配置
   */
    @Override
    @Transactional(readOnly = true)
    public SysReportConfigVo getOne(SysReportConfigBo bo, Function<LambdaQueryWrapper<SysReportConfig>, Boolean>
        func) {
        LambdaQueryWrapper<SysReportConfig> lqw = buildQueryWrapper(bo);
        func.apply(lqw);
        lqw.last("limit 0 offset 1");
        SysReportConfigVo entity = this.baseMapper.selectVoOne(lqw);
        if (entity == null) {
            return entity;
        }
        return fill(entity);
    }

    /**
     * 查询报配置
     */
    @Override
    @Transactional(readOnly = true)
    public SysReportConfigVo getOne(SysReportConfigBo bo) {

        return getOne(bo, lambdaQueryWrapper -> true);
    }

    /**
     * 查询报配置
     */
    @Override
    @Transactional(readOnly = true)
    public SysReportConfigVo queryById(Long id){
        SysReportConfigVo entity=baseMapper.selectVoById(id);
        if (entity == null) {
            return entity;
        }
        return fill(entity);
    }


    /**
   * 查询报配置列表
   */
    @Override
    @Transactional(readOnly = true)
    public TableDataInfo<SysReportConfigVo> queryPageList(SysReportConfigBo bo, PageQuery pageQuery) {

        return queryPageList(bo, pageQuery, lambdaQueryWrapper -> true);
    }

    /**
     * 查询报配置列表
     */
    @Override
    @Transactional(readOnly = true)
    public TableDataInfo<SysReportConfigVo> queryPageList(SysReportConfigBo bo, PageQuery pageQuery,
                                                         Function<LambdaQueryWrapper<SysReportConfig>, Boolean>
                                                             func) {
        LambdaQueryWrapper<SysReportConfig> lqw = buildQueryWrapper(bo);
        func.apply(lqw);
        Page<SysReportConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<SysReportConfigVo> list = result.getRecords();
        list.forEach(entity -> {
            fill(entity);
        });
        return TableDataInfo.build(result);
    }


    /**
     * 查询报配置列表
     */
    @Override
    @Transactional(readOnly = true)
    public List<SysReportConfigVo> queryList(SysReportConfigBo bo) {
        return queryList(bo, lambdaQueryWrapper -> true);
    }

    /**
     * 查询报配置列表
     */
    @Override
    @Transactional(readOnly = true)
    public List<SysReportConfigVo> queryList(SysReportConfigBo bo,
                                            Function<LambdaQueryWrapper<SysReportConfig>, Boolean>
                                                func) {
        LambdaQueryWrapper<SysReportConfig> lqw = buildQueryWrapper(bo);
        func.apply(lqw);
        List<SysReportConfigVo> list = baseMapper.selectVoList(lqw);
        if (list == null) {
            return new ArrayList<SysReportConfigVo>();
        }
        list.forEach(entity -> {
            fill(entity);
        });
        return list;
    }

    public SysReportConfigVo fill(SysReportConfigVo entity) {
        return entity;
    }

    private LambdaQueryWrapper<SysReportConfig> buildQueryWrapper(SysReportConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysReportConfig> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysReportConfig::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), SysReportConfig::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), SysReportConfig::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getContentType()), SysReportConfig::getContentType, bo.getContentType());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), SysReportConfig::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getHeader()), SysReportConfig::getHeader, bo.getHeader());
        lqw.eq(StringUtils.isNotBlank(bo.getSearchConfig()), SysReportConfig::getSearchConfig, bo.getSearchConfig());
        lqw.eq(StringUtils.isNotBlank(bo.getCls()), SysReportConfig::getCls, bo.getCls());
        lqw.eq(StringUtils.isNotBlank(bo.getFilter()), SysReportConfig::getFilter, bo.getFilter());
        lqw.eq(StringUtils.isNotBlank(bo.getFilterSql()), SysReportConfig::getFilterSql, bo.getFilterSql());
        lqw.eq(StringUtils.isNotBlank(bo.getFilterSql1()), SysReportConfig::getFilterSql1, bo.getFilterSql1());
        lqw.eq(StringUtils.isNotBlank(bo.getFilterSql2()), SysReportConfig::getFilterSql2, bo.getFilterSql2());
        lqw.eq(StringUtils.isNotBlank(bo.getFilterSql3()), SysReportConfig::getFilterSql3, bo.getFilterSql3());
        lqw.eq(bo.getRowHeight() != null, SysReportConfig::getRowHeight, bo.getRowHeight());
        lqw.eq(StringUtils.isNotBlank(bo.getMenuConfig()), SysReportConfig::getMenuConfig, bo.getMenuConfig());
        return lqw;
    }

    /**
     * 新增报配置
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(SysReportConfigBo bo) {
        SysReportConfig add = MapstructUtils.convert(bo, SysReportConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改报配置
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(SysReportConfigBo bo) {
        SysReportConfig update = MapstructUtils.convert(bo, SysReportConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysReportConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
    * 删除页面
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidById(Long id, Boolean isValid) {
        return deleteWithValidByIds(CollUtil.newHashSet(id), isValid);
    }
    /**
     * 批量删除报配置
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
